home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 …ember: Reference Library / Apple Developer Reference Library (December 1999) (Disk 1).iso / pc / technical documentation / develop / develop issue 26 / develop issue 26 code / truffles - display mgr. / superfly source / gdeviceutilities.cp next >
Encoding:
Text File  |  1996-02-06  |  4.9 KB  |  219 lines

  1. /*
  2.     File:        GDeviceUtilities.cp
  3.  
  4.     Contains:    Lame utilities to deal with GDevices
  5.                 
  6.     Written by: Kent Miller
  7.     
  8.     Copyright:    © 1995 Apple Computer
  9.  
  10.     Change History (most recent first):
  11.  
  12.  */
  13.  
  14. #include <Displays.h>
  15.  
  16. //SuperFly Includes
  17. #include "GDeviceUtilities.h"
  18. #include "LinkedList.h"
  19.  
  20. SInt16 CountActiveScreenDevices(void)
  21.     {
  22.     GDHandle     theGD;
  23.     SInt16        count = 1;
  24.     
  25.     theGD = DMGetFirstScreenDevice(true);
  26.  
  27.     while ( theGD )
  28.         {
  29.         theGD = DMGetNextScreenDevice(theGD, true);
  30.         if (theGD)
  31.             count++;
  32.         }
  33.  
  34.     return count;
  35.     }
  36.     
  37.  
  38. //returns the offset to (almost) center r2 in r1
  39. void CenterRectInRect (Rect *r1, Rect *r2, SInt16 *hOffset, SInt16 *vOffset)
  40.     {
  41.     SInt16        hCenter1, hCenter2,
  42.                 vCenter1, vCenter2;
  43.     
  44.     //Find global center of first rectangle
  45.     hCenter1 = r1->left + ((r1->right - r1->left) / 2);
  46.     vCenter1 = r1->top + ((r1->bottom - r1->top) / 2);
  47.     
  48.     //Find global center of second rectangle
  49.     hCenter2 = r2->left + ((r2->right - r2->left) / 2);
  50.     vCenter2 = r2->top + ((r2->bottom - r2->top) / 2);
  51.     
  52.     *hOffset = hCenter1 - hCenter2;
  53.     *vOffset = vCenter1 - vCenter2;
  54.     }
  55.  
  56. TLinkedList *BuildAListOfUniqueDevices(void)
  57. //Routine to build a linked list of devices that are "unique" with regard to
  58. //their coordinates.
  59. {
  60.     GDHandle         tempGD, mirroredGD;
  61.     TLinkedList        *myGDList;
  62.  
  63.     myGDList = new TLinkedList;
  64.     
  65.     //First build a linked list with all the GDHandles
  66.     tempGD = DMGetFirstScreenDevice( dmOnlyActiveDisplays );
  67.     while (tempGD)
  68.         {
  69.         myGDList->AddToList(tempGD);
  70.         tempGD = DMGetNextScreenDevice( tempGD, dmOnlyActiveDisplays );  
  71.         }
  72.     
  73.     //Then, walk the list again and remove all the devices that the display manager
  74.     //tells us are mirrored to this one.
  75.     tempGD = (GDHandle) myGDList->GetFirstListElem();
  76.  
  77.     while (tempGD)
  78.         {
  79.         mirroredGD = tempGD;
  80.           //Display manager will give us devices in a loop, so we need to stop when we
  81.           //get the original one back.
  82.         while ((noErr == DMGetNextMirroredDevice( mirroredGD, &mirroredGD )) && (mirroredGD != tempGD))
  83.             myGDList->RemoveFromList(mirroredGD);
  84.             
  85.         tempGD = (GDHandle) myGDList->GetNextListElem(tempGD);
  86.         }
  87.  
  88.     return myGDList;
  89. }
  90.  
  91. SInt16 CountUniqueDeviceRects(void)
  92. //Count the number of unique device gdRects.
  93. //That is, if 2 display are mirrored their gdRects overlap, so they only count once
  94. //in this function.
  95.     {
  96.     TLinkedList        *myGDList;
  97.     SInt16            uniqueCount;
  98.     
  99.     myGDList = (TLinkedList *) BuildAListOfUniqueDevices();
  100.  
  101.     uniqueCount = myGDList->CountListItems();
  102.     
  103.     delete    myGDList;
  104.     
  105.     return     uniqueCount;
  106.     }
  107.  
  108.  
  109. Boolean IsThisDeviceOverlapped(DisplayIDType theDisplayID)
  110. //Build a region out of all active GDevices except the one we're interested in.
  111. //Then, if the device we're interested in intersects with that region, then it must be
  112. //overlapped.
  113. //
  114. //Another way to do this would to use the Display Manager GetNextMirroredDevice call.
  115.     {
  116.     GDHandle         tempGD, thisWindowsGD;
  117.     RgnHandle         desktopRgn, tempRgn;
  118.     Boolean         overlapped = false;
  119.     
  120.     desktopRgn     = NewRgn();
  121.     tempRgn        = NewRgn();
  122.     
  123.     if ( (desktopRgn != nil) && (tempRgn != nil) )
  124.         {
  125.         //Don't give me the main device if you fail
  126.         DMGetGDeviceByDisplayID( theDisplayID, &thisWindowsGD, false );
  127.     
  128.         tempGD = DMGetFirstScreenDevice( dmOnlyActiveDisplays ); 
  129.         
  130.         //Build a region of all active devices except the one I'm interested in
  131.         while ( tempGD )
  132.             {
  133.             if ( tempGD != thisWindowsGD )
  134.                 {
  135.                 RectRgn ( tempRgn, &(**tempGD).gdRect );
  136.                 UnionRgn ( desktopRgn, tempRgn, desktopRgn );
  137.                 }
  138.             //Next active GDDevice
  139.             tempGD = DMGetNextScreenDevice( tempGD, dmOnlyActiveDisplays );  
  140.             }
  141.         
  142.         RectRgn ( tempRgn, &(**thisWindowsGD).gdRect );
  143.         SectRgn ( desktopRgn, tempRgn, tempRgn );
  144.         if (! EmptyRgn(tempRgn) )
  145.             overlapped = true;    
  146.         
  147.         DisposeRgn(desktopRgn);
  148.         DisposeRgn(tempRgn);
  149.         }
  150.         
  151.     return overlapped;
  152.     }
  153.     
  154. void MirrorAllDisplays(Boolean mirroringOn)
  155. {
  156. GDHandle     firstGD, nextGD;
  157. Handle        displayState;
  158.  
  159. firstGD = DMGetFirstScreenDevice( dmOnlyActiveDisplays );
  160. nextGD = firstGD;
  161. displayState = nil;
  162.  
  163. if (DMBeginConfigureDisplays(&displayState))
  164.     return;
  165.  
  166. if (mirroringOn)
  167.     {
  168.     while (nextGD)
  169.         {
  170.         (void) DMMirrorDevices(firstGD, nextGD, displayState);
  171.         nextGD = DMGetNextScreenDevice(nextGD, dmOnlyActiveDisplays);
  172.         }
  173.     }
  174. else
  175.     {
  176.     while (nextGD)
  177.         {
  178.         (void) DMUnmirrorDevice(nextGD, displayState);
  179.         nextGD = DMGetNextScreenDevice(nextGD, dmOnlyActiveDisplays);
  180.         }
  181.     }
  182.  
  183. (void) DMEndConfigureDisplays(displayState);
  184.  
  185. }
  186.  
  187.  
  188. Boolean
  189. MenuBarOnThisScreen(GDHandle thisGD)
  190. {
  191. GDHandle     nextGD;
  192. Boolean        mainDevice = false;    
  193. OSErr        err;
  194.  
  195. //Is this device the main device
  196. if (TestDeviceAttribute (thisGD, mainScreen)) //Account for the menu bar
  197.     mainDevice = true;
  198. else
  199.     {
  200.     nextGD = thisGD;
  201.     do
  202.         {
  203.         err = DMGetNextMirroredDevice(nextGD, &nextGD);
  204.         if (err != noErr)  //mirroring may not be supported by current software
  205.             break;
  206.         
  207.         if (nextGD != nil)
  208.             if (TestDeviceAttribute (nextGD, mainScreen)) //Account for the menu bar
  209.                 {
  210.                 mainDevice = true;
  211.                 break;
  212.                 }
  213.         } while ((nextGD != nil) && (nextGD != thisGD));
  214.  
  215.     }
  216.  
  217. return mainDevice;
  218.  
  219. }